home *** CD-ROM | disk | FTP | other *** search
/ BBS Toolkit / BBS Toolkit.iso / doors_1 / doorskl3.zip / SELECT1.C < prev    next >
C/C++ Source or Header  |  1991-12-15  |  12KB  |  413 lines

  1. /*
  2.  * More fancy input routines.  select_one is ANSI or ASCII.  top_menu is
  3.  * ANSI only.
  4.  *
  5.  */
  6.  
  7. #include "doorskel.h"
  8. #include "keys.h"
  9.  
  10.  
  11.  
  12. int _fastcall select_one (char **selections,int numselecs,char *prompt,
  13.                           unsigned int flags) {
  14.  
  15.     int          x = 0,y,oldx,oldlen,longest = 0,perline = 1,timeout = 0;
  16.     unsigned int temp;
  17.     time_t       startt;
  18.  
  19.     /* 'rolling' input, ASCII or ANSI.  returns -1 (aborted) or # of option
  20.         selected.
  21.  
  22.        bitmapped flags:
  23.  
  24.         1:  Show initial list of options
  25.         2:  Show initial list of first letters
  26.         4:  No roll, just regular hot input
  27.     */
  28.  
  29.  
  30.     if(flags & 1) goto Options;
  31.  
  32. RePrompt:
  33.  
  34.     if(flags & 2) {
  35.  
  36.         int x;
  37.  
  38.         printm("\r\n[");
  39.         for(x = 0;x < numselecs;x++) {
  40.             if(*selections[x] != '*') printfm("%c",*selections[x]);
  41.         }
  42.         printm("?] ");
  43.     }
  44.     if(prompt && *prompt) printm(prompt);
  45.  
  46.     for(;;) {
  47.         getxbbstime();
  48.         if(!(flags & 4)) {
  49.             oldx = x;
  50.             oldlen = printm(selections[x]);
  51.         }
  52.  
  53. Another:
  54.  
  55.         startt = getxbbstime() + 241L;
  56.         do {
  57.             if(startt < getxbbstime()) return -1;
  58.             temp = (int)inkey();
  59.         } while(!temp);
  60.  
  61.         if(temp == '\r') {
  62.             if(flags & 4) {
  63.                 return 0;   /* default to initial option */
  64.             }
  65.             else {
  66.                 return x;   /* return current option */
  67.             }
  68.         }
  69.  
  70.         if(!(flags & 4)) {
  71.             switch(temp) {
  72.                 case PGUP:
  73.                 case UP:
  74.                 case RIGHT:
  75.                 case '9':
  76.                 case '8':
  77.                 case '6':
  78.                 case ' ':
  79.                   x++;
  80.                   if(x >= numselecs) x = 0;
  81.                   goto BotLoop;
  82.  
  83.                 case DN:
  84.                 case LEFT:
  85.                 case PGDN:
  86.                 case '3':
  87.                 case '4':
  88.                 case '2':
  89.                   x--;
  90.                   if(x < 0) x = numselecs - 1;
  91.                   goto BotLoop;
  92.  
  93.                 case END:
  94.                 case '1':
  95.                   x = numselecs - 1;
  96.                   goto BotLoop;
  97.  
  98.                 case HOME:
  99.                 case '7':
  100.                   x = 0;
  101.                   goto BotLoop;
  102.  
  103.                 default:    break;
  104.             }
  105.         }
  106.  
  107.         switch(temp) {
  108.           case '?':
  109.  
  110. Options:
  111.  
  112.             printm("\r\nAvailable options:\r\n");
  113.             {
  114.  
  115.                 int x;
  116.  
  117.                 if(!longest) {
  118.                     for(x = 0;x < numselecs;x++) {
  119.                         y = strlen(selections[x]);
  120.                         longest = max(longest,y);
  121.                     }
  122.                     perline = width / (longest + 2);
  123.                 }
  124.  
  125.                 for(x = 0;x < numselecs;x++) {
  126.                     if(longest <= ((int)width / perline) - 1) {
  127.                         if((x + 1) % perline) {
  128.                             printm(" ");
  129.                             printm(selections[x]);
  130.                             if(x < numselecs - 1) {
  131.                                 for(y = 0;y < (((int)width / perline) - 1) - strlen(selections[x]);y++) {
  132.                                     printm(" ");
  133.                                 }
  134.                             }
  135.                             else printm("\r\n");
  136.                         }
  137.                         else {
  138.                             printfm(" %s\r\n",selections[x]);
  139.                         }
  140.                     }
  141.                     else printfm(" %s\r\n",selections[x]);
  142.                 }
  143.             }
  144.             goto RePrompt;
  145.  
  146.           case ESC:
  147.           case CTRL_K:
  148.             return -1;
  149.  
  150.           default:
  151.             temp = toupper(temp);
  152.             for(y = 0;y < numselecs;y++) {
  153.                 if(toupper(*selections[y]) == (int)temp) {
  154.                     if(!(flags & 4)) {
  155.                         if(y != oldx) {
  156.                             backup(oldlen);
  157.                             printm(selections[y]);
  158.                         }
  159.                     }
  160.                     else printfm("%c",*selections[y]);
  161.                     return y;
  162.                 }
  163.             }
  164.  
  165.             if(!(flags & 4)) {
  166.                 if(graphics) {
  167.                     printm("\r\x1b[KKeypad/cursor toggles, [Enter] selects, ? lists, [ESC/CTRL-K] aborts");
  168.                     fossil(PURGEIN,0);
  169.                     DosSleep(2250L);
  170.                     printm("\r\x1b[K");
  171.                 }
  172.                 else {
  173.                     printm("\r\nKeypad/cursor toggles, [Enter] selects, ? lists, [ESC/CTRL-K] aborts\r\n");
  174.                     DosSleep(100L);
  175.                     fossil(PURGEIN,0);
  176.                 }
  177.             }
  178.             else {
  179.                 printm("\r\07");
  180.                 printg("\x1b[K");
  181.                 fossil(PURGEIN,0);
  182.             }
  183.             goto RePrompt;
  184.         }
  185.  
  186. BotLoop:
  187.  
  188.         if(!(flags & 4)) {
  189.             backup(oldlen);
  190.         }
  191.         else if(isprint(temp)) {
  192.             backup(1);
  193.         }
  194.     }
  195. }
  196.  
  197.  
  198.  
  199. int _fastcall is_one (int key,char **selections,int numselecs) {
  200.  
  201.     /* checks a key to see if it's one of the selections -- -1 = no match */
  202.  
  203.     register int x;
  204.  
  205.  
  206.     if(key && key != -1) {
  207.         for(x = 0;x < numselecs;x++) {
  208.             if(key == (int)*selections[x]) return x;
  209.         }
  210.     }
  211.     return -1;
  212. }
  213.  
  214.  
  215.  
  216. int _fastcall top_menu (char **selections,int numselects) {
  217.  
  218.     int x,ts = 0,first = 0,last,cur = 0,len,redisplay = 1,temp,lastcur = 32767;
  219.     char *more = "<More>";
  220.     time_t startt;
  221.  
  222.     /* top bar menu, ANSI only.  returns -1 (aborted) or # of option chosen */
  223.  
  224.  
  225.     fossil(PURGEIN,0);
  226.  
  227. ReStart:
  228.  
  229.     printm("\x1b[s\x1b[0m");
  230.  
  231.     for(;;) {
  232.         if(redisplay) {
  233.             printm("\x1b[1;1H\x1b[7m\x1b[K ");
  234.             len = 1;
  235.             first = ts;
  236.             for(x = first; ;x++) {
  237.                 len += printm(selections[x]);
  238.                 len += printm("  ");
  239.                 if(x == numselects - 1 ||
  240.                    len > width - (strlen(more) +
  241.                    strlen(selections[x + 1]) + 2)) {
  242.                     last = x;
  243.                     break;
  244.                 }
  245.             }
  246.             last = x;
  247.             if(cur <= first) cur = first;
  248.             else cur = last;
  249.             if(first > 0 || last < numselects - 1) len += printm(more);
  250.             while(len++ < width) printm(" ");
  251.         }
  252.  
  253.         if(cur != lastcur || redisplay) {
  254.             len = 1;
  255.             for(x = first;x <= last;x++) {
  256.                 if(x == cur) {
  257.                     printfm("\x1b[1;%dH\x1b[0m%s\x1b[7m",len + 1,selections[x]);
  258.                     if(redisplay || lastcur == 32767) break;
  259.                 }
  260.                 if(x == lastcur) {
  261.                     printfm("\x1b[1;%dH%s",len + 1,selections[x]);
  262.                 }
  263.                 len += strlen(selections[x]) + 2;
  264.             }
  265.             redisplay = 0;
  266.             lastcur = cur;
  267.         }
  268.  
  269. ReInput:
  270.  
  271.         startt = getxbbstime() + 241L;
  272.         do {
  273.             if(startt < getxbbstime()) return -1;
  274.             temp = (int)inkey();
  275.         } while(!temp);
  276.         switch(temp) {
  277.             case ESC:
  278.             case CTRL_K:
  279.                 printm("\x1b[0m\x1b[u");
  280.                 return -1;
  281.  
  282.             case '\r':
  283.                 printm("\x1b[0m\x1b[u");
  284.                 return cur;
  285.  
  286.             case '4':
  287.             case LEFT:
  288.                 if(!cur) {
  289.                     cur = numselects - 1;
  290.                     len = 1;
  291.                     for(x = numselects - 1; ;x--) {
  292.                         len += strlen(selections[x]) + 2;
  293.                         if(!x ||
  294.                            len > width - (strlen(more) +
  295.                            strlen(selections[x - 1]) + 2)) {
  296.                             ts = x;
  297.                             break;
  298.                         }
  299.                     }
  300.                     if(ts) redisplay = 1;
  301.                     break;
  302.                 }
  303.                 else cur--;
  304.                 if(cur >= first && cur <= last) break; /* else fallthrough to UP handler */
  305.  
  306.             case '8':
  307.             case '9':
  308.             case PGUP:
  309.             case UP:
  310.                 if(first > 0 || last < numselects - 1) {
  311.                     if(ts) {
  312.                         len = 1;
  313.                         for(x = ts - 1; ;x--) {
  314.                             len += strlen(selections[x]) + 2;
  315.                             if(!x ||
  316.                                len > width - (strlen(more) +
  317.                                strlen(selections[x - 1]) + 2)) {
  318.                                 cur = ts - 1;
  319.                                 ts = x;
  320.                                 break;
  321.                             }
  322.                         }
  323.                         redisplay = 1;
  324.                         break;
  325.                     } /* else fallthrough to END handler */
  326.                 }
  327.                 else {
  328.                     printm("\07");
  329.                     break;
  330.                 }
  331.  
  332.             case '1':
  333.             case END:
  334.                 if(first > 0 || last < numselects - 1) {
  335.                     cur = numselects - 1;
  336.                     len = 1;
  337.                     for(x = numselects - 1; ;x--) {
  338.                         len += strlen(selections[x]) + 2;
  339.                         if(!x ||
  340.                            len > width - (strlen(more) +
  341.                            strlen(selections[x - 1]) + 2)) {
  342.                             ts = x;
  343.                             break;
  344.                         }
  345.                     }
  346.                     redisplay = 1;
  347.                 }
  348.                 else printm("\07");
  349.                 break;
  350.  
  351.             case ' ':
  352.             case '6':
  353.             case RIGHT:
  354.                 cur++;
  355.                 if(cur > numselects - 1) {
  356.                     cur = 0;
  357.                     ts = 0;
  358.                     if(first > 0) redisplay = 1;
  359.                     break;
  360.                 }
  361.                 if(cur <= last) break; /* else fallthrough to DN handler */
  362.  
  363.             case '2':
  364.             case '3':
  365.             case PGDN:
  366.             case DN:
  367.                 if(first > 0 || last < numselects - 1) {
  368.                     ts = last + 1;
  369.                     if(ts > numselects - 1) ts = 0;
  370.                     redisplay = 1;
  371.                 }
  372.                 else printm("\07");
  373.                 break;
  374.  
  375.             case '7':
  376.             case HOME:
  377.                 cur = 0;
  378.                 ts = 0;
  379.                 redisplay = 1;
  380.                 break;
  381.  
  382.             default:
  383.                 temp = toupper(temp);
  384.                 for(x = first;x < last + 1;x++) {   /* first try what's showing */
  385.                     if(toupper(*selections[x]) == temp) {
  386.                         printm("\x1b[0m\x1b[u");
  387.                         return x;
  388.                     }
  389.                 }
  390.                 for(x = 0;x < numselects;x++) {     /* then try everything */
  391.                     if(toupper(*selections[x]) == temp) {
  392.                         printm("\x1b[0m\x1b[u");
  393.                         return x;
  394.                     }
  395.                 }
  396.                 printm("\07");
  397.                 goto ReInput;
  398.         }
  399.     }
  400.  
  401.     printm("\x1b[0m\x1b[u");
  402.     return -1;
  403. }
  404.  
  405.  
  406.  
  407. void _fastcall backup (int num) {
  408.  
  409.     /* prints num number of backspaces to overwrite a string */
  410.  
  411.     while(num--) printm(BACKSPACE);
  412. }
  413.